<?php

namespace Biz\module\Classes;

use Biz\model\salesRebate\SalesAccount as mSalesAccount;
use Biz\model\salesRebate\SalesCompany as mSalesCompany;
use Biz\model\salesRebate\SalesCoupon as mSalesCoupon;
use Biz\model\salesRebate\SalesOrder as mSalesOrder;
use Biz\model\salesRebate\SalesPersonal as mSalesPersonal;
use Biz\model\salesRebate\SalesWithdrawalRecord as mSalesWithdrawalRecord;
use Doraemon\model\Phone as mPhone;
use Doraemon\service\Member\PhoneCrypto;
use Doraemon\service\User as sUser;

class SalesRebate
{
    /**
     * 获取返佣比例
     * return array
     */
    public static function getRatio($uid)
    {
        $zero = '0.00';
        $account = mSalesAccount::getAllByWhere('*', ['uid' => $uid]);
        $amount = 0;
        $accountList = [];
        foreach ($account as $_account) {
            if ($_account['type'] == 1) {
                $accounts['person'] = $_account['balance'];
                $amount += $accounts['person'];
            } else {
                $accounts['company'] = $_account['balance'];
                $amount += $accounts['company'];
            }
            $accountList[$_account['type']] = $_account;
        }
        $data['take_amount'] = $amount > 0 ? round($amount, 2) : $zero;
        $data['person_amount'] = isset($accounts['person']) && $accounts['person'] > 0 ? $accounts['person'] : $zero;
        $data['company_amount'] = isset($accounts['company']) && $accounts['company'] > 0 ? $accounts['company'] : $zero;
        return $data;
    }

    /**
     * 是否是有效账户
     * @param $uid
     * @param $type
     * @return array
     */
    public static function getAccount($uid, $type, $status = '')
    {
        $result = [];
        $where = [
            'uid' => $uid,
        ];
        if ($status <> '' && is_array($status)) {
            $where = array_merge($where, $status);
        }
        if ($type == 1) {
            $result = mSalesPersonal::getOneByWhere('*', $where);
        } else {
            $result = mSalesCompany::getOneByWhere('*', $where);
        }
        return $result;
    }

    /**
     * 获取用户信息
     * @param $uid
     * @return array
     */
    public static function getUser($params)
    {
        foreach ($params as $_k => &$_param) {
            if (empty($_param)) {
                unset($params[$_k]);
            }
        }
        $user = [];
        if (isset($params['uid'])) {
            $user = sUser::getUsers(is_array($params['uid']) ? $params['uid'] : [$params['uid']]);
            $userPhone = mPhone::getPhoneList(is_array($params['uid']) ? $params['uid'] : [$params['uid']]);
        } else {
            $userId = mSalesCoupon::getAllByWhere('uid');
            $user = sUser::getUsers($userId);
            $userPhone = mPhone::getPhoneList($userId);
        }
        if (!empty($user)) {
            $user = array_map(function ($_item) use ($userPhone) {
                if (isset($userPhone[$_item['uid']])) {
                    $_item['telephone'] = $userPhone[$_item['uid']]['hidden_phone'];
                    $_item['phone'] = $userPhone[$_item['uid']]['phone'];
                    return $_item;
                }
            }, $user);
            if (isset($params['telephone'])) {
                $user = array_filter($user, function ($_item) use ($params) {
                    $encryptPhone = PhoneCrypto::encrypt($params['telephone'], PhoneCrypto::CLIENT_SHOP_ADMIN, PhoneCrypto::VERSIN);
                    if ($encryptPhone == $_item['phone']) {
                        return $_item;
                    }
                });
            }
            if (isset($params['nick'])) {
                $user = array_filter($user, function ($_item) use ($params) {
                    if ($params['nick'] == $_item['nick']) {
                        return $_item;
                    }
                });
            }
            $user = array_column($user, NULL, 'uid');
        }
        return $user;
    }

    /**
     * @param $status 状态值
     * @param int $model 模块 1白名单，2账户，3提现申请，4订单
     * @param string $type 1 个人，2公司
     * @return string
     */
    public static function getStatus($status, $model = 1, $type = '')
    {
        $result = '';
        switch ($model) {
            case 1:
                switch ($status) {
                    case mSalesCoupon::STATUS_NORMAL:
                        $result = '未启用';
                        break;
                    case mSalesCoupon::STATUS_OPEN:
                        $result = '启用';
                        break;
                }
                break;
            case 2:
                if ($type == 1) {
                    switch ($status) {
                        case mSalesPersonal::STATUS_NORMAL:
                            $result = '签约中';
                            break;
                        case mSalesPersonal::STATUS_SIGNED:
                            $result = '已签约';
                            break;
                    }
                } else if ($type == 2) {
                    switch ($status) {
                        case mSalesCompany::STATUS_NORMAL:
                            $result = '已绑定';
                            break;
                    }
                }
                break;
            case 3:
                if ($type == 1) {
                    switch ($status) {
                        case mSalesWithdrawalRecord::STATUS_NORMAL:
                            $result = '未入账';
                            break;
                        case mSalesWithdrawalRecord::STATUS_ACCOUNTED:
                            $result = '已入账';
                            break;
                        case mSalesWithdrawalRecord::STATUS_INVALID:
                            $result = '已失效';
                            break;
                    }
                } else if ($type == 2) {
                    switch ($status) {
                        case mSalesWithdrawalRecord::STATUS_NORMAL:
                            $result = '未入账，待确认发票';
                            break;
                        case mSalesWithdrawalRecord::STATUS_ACCOUNTING:
                            $result = '入账中，已收发票';
                            break;
                        case mSalesWithdrawalRecord::STATUS_ACCOUNTED:
                            $result = '已入账';
                            break;
                        case mSalesWithdrawalRecord::STATUS_INVALID:
                            $result = '已失效';
                            break;
                    }
                }
                break;
            case 4:
                switch ($status) {
                    case mSalesOrder::STATUS_NORMAL:
                        $result = '未入账';
                        break;
                    case mSalesOrder::STATUS_ACCOUNTED:
                        $result = '已入账';
                        break;
                    case mSalesOrder::STATUS_INVALID:
                        $result = '已失效';
                        break;
                }
                break;
        }
        switch ($status) {
            case 9:
                $result = '已删除';
                break;
        }
        return $result;
    }
}